<template>
	<view>
		<u-sticky>
			<view class="search">
				<input type="search" name="" id="" class="topSearch" placeholder="输入门店名称" v-model="form.name">
				<image src="../../static/searchRight.png" mode="" class="searchBtn" @click="getSearch"></image>
			</view>
		</u-sticky>
		<view class="recommend f-column">
			<view class="list f-column" v-for="(item,index) in list" :key="index" @click="goDetails(item.id)">
				<view class="list_item" :style="{background:'#fff'}">
					<view class="content">
						<view class="item" @click="goCard(item)">
							<view class="icon">
								<image :src="item.is_trila == '0' ? item.logo : '../../static/images/index/fgLogo.png'" mode="" class="image"></image>
							</view>
							<view class="entityList">
								<view class="num name" style="font-size: 32rpx;">
									<text>{{item.name || '--'}}</text>
									<view class="right">
										<image src="../../static/icon/my/goIcon.png" mode="" class="image"></image>
										<text class="themeColor">{{item.distance || 0}}km</text>
									</view>
								</view>
								<view class="name">
									<text>{{item.address}}</text>
								</view>
								<view>
									<text v-if="item.openswitch == 1" class="tag">正常营业</text>
									<text v-if="item.openswitch == 2" class="tag error">未营业</text>
									<text v-if="item.fly_id != '0'" class="tag">空闲:{{ item.machine[1].value || 0}}台</text>
									<text v-if="item.fly_id != '0'" class="tag">总数:{{ item.machine[0].value || 0}}台</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="noMore" v-if="last_page==form.page&&list.length!=0"
			style="text-align: center;color: #797575;margin-top: 20rpx;padding-bottom: 60rpx;">
			数据加载完成
		</view>
		<u-empty text="暂无列表哦~~" mode="list" v-if="list.length==0" margin-top='50'></u-empty>
	</view>
</template>

<script>
	import {
		merchantList
	} from '@/request/api.js'
	export default {
		data() {
			return {
				form: {
					lon: '',
					lat: '',
					page: 1,
					name: ''
				},
				list: [],
				last_page: 1,
			};
		},
		onLoad(res) {
			if (res.type == 'search') {
				uni.setNavigationBarTitle({
					title: '搜索门店'
				})
			} else {
				uni.setNavigationBarTitle({
					title: '优惠门店'
				})
			}
			this.getAuthorizeInfo()
		},
		onReachBottom() {
			if (this.form.page < this.last_page) {
				this.form.page = this.form.page + 1
				this.getList()
			}
		},
		methods: {
			goDetails(e) {
				uni.navigateTo({
					url: `/pages/index/productDetails?id=` + e,
				})
			},
			getSearch() {
				this.form.page = 1
				this.list = []
				this.last_page = 1
				this.getList()
			},
			openAuthSetting() {
				let that = this;
				uni.getSetting({
					success: (res) => {
						console.log(res, JSON.stringify(res, "getSetting"));
						if (
							res.authSetting["scope.userLocation"] != undefined &&
							res.authSetting["scope.userLocation"] != true
						) {
							uni.showModal({
								title: "请求授权当前位置",
								content: "需要获取您的地理位置，请确认授权",
								success: function(res) {
									if (res.cancel) {
										uni.showToast({
											title: "拒绝授权",
											icon: "none"
										});
									} else if (res.confirm) {
										uni.openSetting({
											success: function(dataAu) {
												if (dataAu.authSetting[
														"scope.userLocation"] ==
													true) {
													uni.showToast({
														title: "授权成功",
														icon: "none"
													});
													//再次授权，调用wx.getLocation的API
													that.getAuthorizeInfo();
												} else {
													uni.showToast({
														title: "授权失败",
														icon: "none"
													});
												}
											},
										});
									}
								},
							});
						} else if (res.authSetting["scope.userLocation"] == undefined) {
							//调用wx.getLocation的API
							console.log("首次授权");
							this.getAuthorizeInfo();
						} else { //强制获取位置
							//调用wx.getLocation的API
							this.getAuthorizeInfo();
						}
					},
				})
			},
			getAuthorizeInfo() {
				let self = this
				console.log(111)
				uni.authorize({
					scope: 'scope.userLocation',
					success() { // 允许授权
						uni.getLocation({
							success: function(res) {
								console.log("经纬度", res);
								self.form.lon = res.longitude
								self.form.lat = res.latitude
								self.getList()
							},
						})
					},
					fail() { // 拒绝授权
						self.openAuthSetting()
						console.log("你拒绝了授权，无法获得周边信息")
					}
				})
			},
			async getList() {
				let res = await merchantList(this.form);
				this.list = this.list.concat(res.data.data)
				this.last_page = res.data.last_page
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-sticky {
		padding: 20rpx !important;
		background-color: #fff !important;
	}

	.topSearch {
		border-radius: 80px;
		height: 70rpx;
		line-height: 70rpx;
		display: inherit;
		padding: 0 20rpx;
		color: #BABABA;
		padding-right: 80rpx;
		background: rgba(255, 255, 255, 0.5);
	}

	.searchBtn {
		height: 40rpx;
		width: 40rpx;
		right: 50rpx;
		top: 15rpx;
	}

	.search {
		padding: 0 20rpx;
		position: relative;
		margin: 15rpx 0;
		border: 0.5rpx solid #f2f2f2;
		border-radius: 20px;
		flex: 1;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.recommend {
		padding: 20rpx;

		.title {
			font-size: 30rpx;
			line-height: 20px;
			color: #222222;
			font-weight: 600;
		}

		.list {
			margin-bottom: 20rpx;

			.content {
				.conBot {
					align-items: flex-end;

					.f-row {
						display: flex;
						align-items: flex-end;
					}

					.goNear {
						background: #2CB9A4;
						color: #fff;
						height: 60rpx;
						justify-content: center;
						align-items: center;
						border-radius: 10rpx;
						padding: 10rpx 20rpx;
						font-size: 32rpx;
						align-items: center;
					}

					display: flex;
					justify-content: space-between;
					color: #2CB9A4;

					.down {
						background: #C0EAE4;
						justify-content: center;
						display: flex;
						flex-direction: column;
						align-items: center;
						padding: 10rpx;
						font-size: 22rpx;
						border-radius: 10rpx;
						margin-right: 10rpx;
					}

					.down_price {
						margin-right: 10rpx;
					}

					.price {
						margin-right: 10rpx;
					}
				}
			}

			.list_item {
				display: flex;
				flex-direction: column;
				background: #2CB9A4;
				border-radius: 20rpx;
				padding: 0 20rpx;

				.title {
					color: #fff;
					font-size: 30rpx;
					font-weight: 600;
				}

				.item {
					display: flex;
					color: #505050;
					margin: 15rpx 0;
					border-bottom: 0.5px solid rgba(217, 217, 217, 0.3);
					padding-bottom: 15rpx;
					box-sizing: border-box;
					background: #fff;
					border-radius: 20rpx;

					.icon {
						width: 140rpx;
						height: 140rpx;

						.image {
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}

					.entityList {
						flex: 1;
						display: flex;
						flex-direction: column;
						overflow: hidden;
						text-overflow: ellipsis;
						justify-content: space-between;

						.name {
							display: flex;
							justify-content: space-between;
							margin-left: 15rpx;
							// white-space: nowrap;
							// overflow: hidden;
							// text-overflow: ellipsis;
							color: #BABABA;
							font-size: 28rpx;

							.image {
								width: 26rpx;
								height: 26rpx;
								margin-right: 10rpx;
							}
						}

						.tag {
							background-color: #2cb9a4;
							padding: 4rpx 10rpx;
							border-radius: 6rpx;
							font-size: 24rpx;
							color: #fff;
							display: inline-block;
							margin-left: 16rpx;
						}
						.error {
							background-color: #FF6666;
						}
						.num {
							margin-left: 15rpx;
							margin-top: 5rpx;
							color: #2CB9A4;
							font-size: 28rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
				}
			}
		}
	}
</style>